# main_window.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
from models import db, Equipment
from flask import Flask

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///equipment.db'
db.init_app(app)

class EquipmentWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("设备管理系统")
        self.resize(800, 600)
        self.table = QTableWidget()
        self.setCentralWidget(self.table)
        self.load_data()

    def load_data(self):
        with app.app_context():
            equipments = Equipment.query.limit(500).all()  # 加载前500条数据
        self.table.setRowCount(len(equipments))
        self.table.setColumnCount(6)
        self.table.setHorizontalHeaderLabels(['设备名称', '使用单位', '相机模块', '运动模块', '操作系统','软件版本'])
        for row, eq in enumerate(equipments):
            self.table.setItem(row, 0, QTableWidgetItem(eq.equip_name))
            self.table.setItem(row, 1, QTableWidgetItem(eq.equip_user))
            self.table.setItem(row, 2, QTableWidgetItem(eq.camera_type))
            self.table.setItem(row, 3, QTableWidgetItem(eq.motion_type))
            self.table.setItem(row, 4, QTableWidgetItem(eq.os_type))
            self.table.setItem(row, 5, QTableWidgetItem(eq.software_ver))

if __name__ == '__main__':
    qt_app = QApplication(sys.argv)
    window = EquipmentWindow()
    window.show()
    sys.exit(qt_app.exec_())